/**
 * 2016年8月8日
 */
package dao.web.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dao.BaseDao;
import dao.web.WebContentDao;
import pojo.web.WebContent;
import util.Page;

/**
 * @author Alex
 *
 */
@Repository
public class WebContentDaoImpl extends BaseDao implements WebContentDao{

	/* (non-Javadoc)
	 * @see dao.web.WebContentDao#insertLabel(pojo.web.WebContent)
	 */
	public void insertWebContent(WebContent web) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(web);
		
	}

	/* (non-Javadoc)
	 * @see dao.web.WebContentDao#selectWebContentById(java.lang.Integer)
	 */
	public List<WebContent> selectWebContentById(Integer id) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(WebContent.class);
		dc.add(Restrictions.eq("id",id));
		
		return (List<WebContent>) this.getHibernateTemplate().findByCriteria(dc);
	}

	/* (non-Javadoc)
	 * @see dao.web.WebContentDao#selectWebContentByDate(util.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<WebContent> selectWebContentByDate(Page page) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(WebContent.class);
		dc.add(Restrictions.sqlRestriction("1=1 order by rand()"));
//		dc.addOrder(Order.desc("date"));	
		return  (List<WebContent>) this.getHibernateTemplate().findByCriteria(dc,page.getBeginIndex(),page.getCurrentPage() * page.getEveryPage());
	}

}
